import redisHandle from "./redisHandle"
import app from 'src/app'
var ObjectId = require('mongodb').ObjectId;
import moment = require("moment")
import mongodbHandle from "./mongodbHandle"
import playerHandle from "core/handle/playerHandle"
import Random from "core/utils/random"
import { AreaCsModel } from "core/model/areaCsModel";

class AreaCsHandle {
    constructor() {
    }
    async initAreaCsData() {
        let data = await redisHandle.get(app.config.gameName + 'areaCsData')
        console.log(JSON.stringify(data).length)
        if (!data) {
            let areaCsData = new AreaCsModel()
            areaCsData.day = moment().startOf('day').valueOf()
            await redisHandle.set(app.config.gameName + 'areaCsData', JSON.stringify(areaCsData))
        } else {
            let areaCsData = new AreaCsModel(JSON.parse(data))
            await this.saveAreaCsData(areaCsData)
        }
    }

    async saveAreaCsData(areaCsData: AreaCsModel) {
        await this.getAreaCsData()
        areaCsData.day = moment().startOf('day').valueOf()
        await redisHandle.set(app.config.gameName + 'areaCsData', JSON.stringify(areaCsData))
        return areaCsData
    }

    async getAreaCsData() {
        let data = await redisHandle.get(app.config.gameName + 'areaCsData')
        if (data) {
            let areaCsData: AreaCsModel = JSON.parse(data)
            if (areaCsData.day !== moment().startOf('day').valueOf()) {
                await redisHandle.set(app.config.gameName + 'preAreaCsData', JSON.stringify(areaCsData))
            }
            return areaCsData
        } else {
            let areaCsData = new AreaCsModel()
            return areaCsData
        }
    }

    async getPreAreaCsData() {
        let data = await redisHandle.get(app.config.gameName + 'preAreaCsData')
        if (data) {
            let areaCsData: AreaCsModel = JSON.parse(data)
            if (areaCsData.day !== moment().add('day', -1).startOf('day').valueOf()) {
                await this.getAreaCsData()
                data = await redisHandle.get(app.config.gameName + 'preAreaCsData')
                let areaCsData: AreaCsModel = JSON.parse(data)
                if (areaCsData.day !== moment().add('day', -1).startOf('day').valueOf()) {
                    return null
                } else {
                    return areaCsData
                }
            }
            return areaCsData
        } else {
            return null
        }
    }

    saveAreaCsBattle = async (pid1, pid2, cID) => {

        let player1 = await mongodbHandle.playerDao.findOne({ _id: ObjectId(pid1) })
        let player2 = await mongodbHandle.playerDao.findOne({ _id: ObjectId(pid2) })
        if (!player1 || !player2) {
            return
        }
        let random = new Random()
        let seed = random.getSeed()
        await mongodbHandle.areaCsDao.newAndSave(player1.name, player2.name, pid1, pid2,
            player1.currentServerID, player2.currentServerID, 1, seed, app.version, cID)
        return
    }


    getZoneByCID(cID) {
        return Math.floor((cID - 1) / 100) + 1
    }

}



let areaCsHandle = new AreaCsHandle()
export default areaCsHandle